<template>
  <div class="mainua">
	<div class="main_spac1" style="padding-bottom:5px;">
	  <div class="main_white padding10">
		<div class="main_crumbs boxdq">
		  <div class="main_crumbs_l">
			<a href="javascript:;" @click="$router.back(-1)">
			  <i class="el-icon-arrow-down"></i>返回
			</a>
			<em>|</em>
			<span>{{title}}</span>
		  </div>
		</div>
		<div class="main_tile boxdq marginTop10">
		  <div class="main_tile_l">
			<em></em>基本信息
		  </div>
		</div>
		<div class="maincla_form">
		  <el-form
			:inline="true"
			:rules="rules"
			:model="monitorForm"
			class="demo-form-inline"
			ref="userListFrom"
			label-width="110px"
		  >
			<div>
			  <el-form-item label="商品名称：" prop="form.name" :rules="rules.name">
				<el-input
				  v-model.trim="monitorForm.form.name"
				  placeholder="商品名称"
				  clearable
				  :disabled="JSON.parse(nostand)"
				  class="wid240"
				  @blur.prevent="((val)=>{inputBlur(val,'main')})"
				  @keyup.enter.native="$event.target.blur"
				  @input="inputName"
				></el-input>
			  </el-form-item>
			  <el-form-item
				label="助记码："
				prop="form.pinyin"
				:rules="rules.pinyin"
			  >
				<el-input
				  v-model.trim="monitorForm.form.pinyin"
				  placeholder="默认拼音码"
				  clearable
				  class="wid240"
				></el-input>
			  </el-form-item>
			  <div class="clearfix"></div>
<!-- 其它名称 -->

			  <el-form-item class="name_list"  label="其它名称：" >
					<div v-for="(item,index) in nameList" :key="index" style="display:flex;">
						<el-input
							v-model.trim="nameList[index].name"
							placeholder="商品别名"
							clearable
							class="wid240"
							@change="checkName"
							@blur="((val)=>{inputBlur(val,'other',index)})"
						></el-input>
						<div class="maincla_sxf">
							<svg
							aria-hidden="true"
							class="main_page_svg"
							width="15"
							height="15"
							@click="AddOrSubNameList()"
							>
							<use xlink:href="#icon-commodity_plus" />
							</svg>
							<svg
							aria-hidden="true"
							class="main_page_svg"
							width="22"
							height="22"
							v-if="index!=0"
							@click="AddOrSubNameList(index)"
							>
							<use xlink:href="#icon-commodity_reduce" />
							</svg>
						</div>

					</div>
					<div v-if="nameSame" style="color:#F56C6C;font-size:12px;">名称重复</div>
			  </el-form-item>
			  
			  <div class="clearfix"></div>
			  <el-form-item
				label="商品分类："
				prop="form.category_id"
				:rules="rules.category_id"
			  >
				<el-cascader
				class="wid240"
				  v-model="monitorForm.form.category_id"
				  :options="options"
				  :disabled="tally > 0"
				  :props="defaultProps"
				></el-cascader>
			  </el-form-item>
			  
			  <el-form-item
				label="商品品牌："
				prop="form.brand"
			  >
				<el-select v-model.trim="monitorForm.form.brand" clearable placeholder="商品品牌" class="wid240">
				  <el-option
					v-for="(brand,index) in info.brand"
					:key="index"
					:label="brand.title"
					:value="brand.id"
				  ></el-option>
				</el-select>
			  </el-form-item>
			  <div class="clearfix"></div>
			  <el-form-item v-if="nostand" label="编码(SPU)：" prop="form.goods_code" :rules="rules.standard_code">
				<div class="box">
				  <el-input
					v-model.trim="monitorForm.form.goods_code"
					placeholder="系统自动生成"
					clearable
					disabled
					class="wid240"
				  ></el-input>
				</div>
			  </el-form-item>
			  <el-form-item v-else label="编码(SPU)：" prop="form.standard_code" :rules="rules.standard_code">
				<div class="box">
				  <el-input
					v-model.trim="monitorForm.form.standard_code"
					placeholder="系统自动生成"
					clearable
					disabled
					class="wid240"
				  ></el-input>
				</div>
			  </el-form-item>
			  <el-form-item label="税收分类编码" prop="form.txa_code" class="maincla_te wid240">
				<el-input  placeholder="请输入税收分类编码" v-model="monitorForm.form.txa_code"></el-input>
			  </el-form-item>
			  <div class="clearfix"></div>
			  <el-form-item label="商品描述：" prop="form.describe" class="maincla_te">
				<el-input type="textarea" placeholder="请输入商品描述" v-model="monitorForm.form.describe"></el-input>
			  </el-form-item>
			  <div class="box">
				<el-form-item label="标品：" prop="form.is_standard" label-width="70px">
				  <el-switch
					v-model="monitorForm.form.is_standard"
					active-color="#03A98E"
					active-text="开"
					:active-value="1"
					inactive-text="关"
					:inactive-value="0"
					class="mainor_switch"
				  ></el-switch>
				</el-form-item>
			  </div>
			</div>
			<div class="main_tile boxdq">
			  <div class="main_tile_l">
				<em></em>商品主图及详情
				<span>建议图片尺寸是:302*302，最多三张，主图为默认第一张</span>
			  </div>
			</div>
			<div class="marginTop20 marginLeft10 main_upImg">
			  <el-upload
				:action="uploadPath"
				:data="{store:'goods'}"
				list-type="picture-card"
				:on-success="handSuccess"
				:on-preview="handlePictureCardPreview"
				:on-remove="handleRemove"
				:limit="3"
				:file-list="filelist"
			  >
				<span slot="default">
				  <svg aria-hidden="true" class="main_page_svg" width="32" height="27">
					<use xlink:href="#icon-commodit_camera" />
				  </svg>
				</span>
			  </el-upload>
			  <el-dialog :visible.sync="dialogVisible">
				<img :src="dialogImageUrl" alt />
			  </el-dialog>
			  <div class="clearfix"></div>
			</div>
			<div class="clearfix"></div>
			<div class="maincla_ed">
			  <Editor
				api-key="vl7byj8kh7hyvvi6o7137s7khcpfsp762987dlnbcd93u37a"
				v-model="monitorForm.form.detail"
				:init="init"
			  ></Editor>
			</div>
			<div class="main_tile box marginTop20">
			  <div class="main_tile_l">
				<em></em>SKU设置
			  </div>
			  <div class="maincla_ra" v-if="!show">
				<el-radio-group
				  v-model="monitorForm.form.transfer"
				  :border="true"
				  @change="radioGrou"
				>
				  <el-radio :label="1">可互调</el-radio>
				  <el-radio :label="0">不可互调</el-radio>
				</el-radio-group>
			  </div>
			</div>

			<div class="main_table row-expand-down">
			  <el-table
				ref="multipleTable"
				:data="monitorForm.list"
				element-loading-text="加载中"
				fit
				highlight-current-row
			  >
				<el-table-column type="expand" width="1">
				  <template slot-scope="props">
					<div class="maincla_zh box">
					  <span>1{{ props.row.unit | unit(info) }}约</span>
					  <el-form-item
						:prop="'list.' + props.$index + '.transfer_num'"
						:rules="rules.transfer_num"
						label-width="10px"
						label=" "
					  >
						<el-input
						  v-model.trim="props.row.transfer_num"
						  placeholder
						  clearable
						  class="wid110"
						></el-input>
					  </el-form-item>
					  <el-form-item
						:prop="'list.' + props.$index + '.transfer_unit'"
						:rules="rules.transfer_unit"
						label-width="10px"
						label=" "
						style="margin-left:5px;"
					  >
						<el-select
						  v-model.trim="props.row.transfer_unit"
						  placeholder="规格"
						  class="wid110"
						>
						  <el-option
							v-for="(unit,index) in info.unit"
							:key="index"
							:label="unit.title"
							:value="unit.id"
						  ></el-option>
						</el-select>
					  </el-form-item>
					  <p @click="toogleExpand_qx(props.row,props.$index)">取消转换</p>
					</div>
				  </template>
				</el-table-column>

				<el-table-column
				  label="规格"
				  prop="spu_name"
				  class-name="main_hei"
				  width="360"
				  align="center"
				>
				  <template slot-scope="scope">
					<div class="maincla_sx boxnr">
<!-- 增减行图标 -->
					  <div class="maincla_sxf">
						<svg
						  aria-hidden="true"
						  class="main_page_svg"
						  width="15"
						  height="15"
						  @click="plus"
						>
						  <use xlink:href="#icon-commodity_plus" />
						</svg>
						<svg
						  aria-hidden="true"
						  class="main_page_svg"
						  width="22"
						  height="22"
						  v-if="scope.$index!=0 && scope.row.candel==true"
						  @click="reduce(scope.$index)"
						>
						  <use xlink:href="#icon-commodity_reduce" />
						</svg>
					  </div>
					  <span class="maincla_sxs" >{{ monitorForm.form.transfer==1?(scope.$index==0?'基础':''):'' }}</span>
					  <el-form-item
						:prop="'list.' + scope.$index + '.spu_name'"
						:rules="rules.spu_name"
						label=" "
						label-width="10px"
					  >
					  	<!-- <el-input
							v-model.trim="scope.row.spu_name"
							placeholder="属性"
							clearable
							class="wid110"
						  ></el-input> -->
						<el-select
						  v-model.trim="scope.row.spu_name"
						  placeholder="规格"
						  class="wid140"
						  v-if="monitorForm.form.transfer==0"
						>

						  <el-option
							v-for="(attribute,index) in info.attribute"
							:key="index"
							:label="attribute.title"
							:value="attribute.id"
						  ></el-option>
						</el-select>
						<el-select
						  v-model.trim="scope.row.spu_name"
						  placeholder="规格"
						  class="wid140"
						  v-if="monitorForm.form.transfer==1"
						  @change="scope.row.unit=scope.row.spu_name"
						> 
						  <el-option
							v-for="(unit,index) in info.unit"
							:key="index"
							:label="unit.title"
							:value="unit.id"
						  ></el-option>
						</el-select>
					  </el-form-item>
					</div>
				  </template>
				</el-table-column>
				<el-table-column label="单位" prop="unit" class-name="main_hei" width="200">
				  <template slot-scope="scope">
					<div class="maincla_dw boxnr">
					  <div class="maincla_dwp boxnr">
						<div
						  v-if="monitorForm.form.transfer==1"
						  class="maincla_dwp_un"
						>{{ scope.row.unit | unit(info) }}</div>
						<el-form-item
						  :prop="'list.' + scope.$index + '.unit'"
						  :rules="rules.unit"
						  label-width="10px"
						  label=" "
						  v-if="monitorForm.form.transfer==0"
						>
						  <el-select
							v-model.trim="scope.row.unit"
							placeholder="单位"
							class="wid110"
						  >
							<el-option
							  v-for="(unit,index) in info.unit"
							  :key="index"
							  :label="unit.title"
							  :value="unit.id"
							></el-option>
						  </el-select>
						</el-form-item>
						<!-- <strong @click="toogleExpand(scope.row,scope.$index)">配送转换</strong> -->
					  </div>
					  <!-- <p>
						<el-tooltip
						  class="item"
						  effect="dark"
						  content="示例：1只鸡约1.5斤"
						  placement="top"
						>
						  <i class="el-icon-question"></i>
						</el-tooltip>
					  </p> -->
					</div>
				  </template>
				</el-table-column>
				<el-table-column
				  label="互调比"
				  prop="package_num"
				  :class-name="monitorForm.form.transfer==1?'main_hei':''"
				  width="180"
				>
				  <template slot-scope="scope">
					<div class="maincla_bz">
					  <span v-if="monitorForm.form.transfer==0">{{ scope.row.package_num }}</span>
					  <div class="boxnr" v-else>
						<el-form-item
						  :prop="'list.' + scope.$index + '.package_num'"
						  :rules="rules.package_num"
						  label-width="10px"
						  label=" "
						>
						  <el-input
							v-model.trim="scope.row.package_num"
							placeholder="互调比"
							clearable
							class="wid110"
						  ></el-input>
						</el-form-item>
						<!-- <span class="marginLeft5">{{ monitorForm.list[0].unit | unit(info) }}</span> -->
					  </div>
					</div>
				  </template>
				</el-table-column>
				<el-table-column
				  label="编码sku"
				  prop="goods_spu_code"
				  class-name="main_hei"
				  width="200"
				  v-if="nostand"
				>
				  <template slot-scope="scope">
					<el-form-item
					  :prop="'list.' + scope.$index + '.goods_spu_code'"
					  :rules="rules.goods_spu_code"
					  label-width="10px"
					  label=" "
					>
					  <el-input
						v-model.trim="scope.row.goods_spu_code"
						placeholder="系统自动生成"
						clearable
						disabled
						class="wid120"
					  ></el-input>
					</el-form-item>
				  </template>
				</el-table-column>
				<el-table-column
				  label="编码sku"
				  prop="standard_spu_code"
				  class-name="main_hei"
				  width="200"
				  v-else
				>
				  <template slot-scope="scope">
					<el-form-item
					  :prop="'list.' + scope.$index + '.standard_spu_code'"
					  :rules="rules.standard_spu_code"
					  label-width="10px"
					  label=" "
					>
					  <el-input
						v-model.trim="scope.row.standard_spu_code"
						placeholder="系统自动生成"
						clearable
						disabled
						class="wid120"
					  ></el-input>
					</el-form-item>
				  </template>
				</el-table-column>
			  </el-table>
			</div>

			<div class="clearfix"></div>
			<div class="newConBox_p200 paddingBottom10" id="newConBox">
			  <div class="newConBox_h32">
				<div :class="['boxdq newConBox ',{on:searchBarFixed}]">
				  <div class="mainor_prl">
					<el-button v-if="!id" type="primary" @click="preservation(1)" v-repeatClick>保存并继续添加</el-button>
					<el-button
					  class="reset marginLeft10"
					  type="primary"
					  @click="preservation(2)"
					  v-repeatClick
					>{{saveText}}</el-button>
				  </div>
				</div>
			  </div>
			</div>
		  </el-form>
		</div>
	  </div>
	</div>
  </div>
</template>

<script>
import Editor from "@tinymce/tinymce-vue";
import init from "./init";
import { uploadPath } from "@/api/currency";
import {
  goods_base,
  goods_add,
  goods_check,
  goods_edit,
  standardDetail,
  nonStandardDetail,
  importStandard,
} from "@/api/commodity/commodityList";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { recursion, category } from "@/utils/index";
import vPinyin from "@/utils/pinyin-py";
import rules from "@/rules/commodity/commodity_List_add";

const shuju = {
  list: {
	base: 1,
	spu_name: "",
	unit: "",
	package_num: 1,
	transfer_unit: "",
	transfer_num: 0,
	standard_spu_code: "",
	down: false,
	status: true,
	candel:false,	//是否能删除
  },
  form: {
	name: "",
	name_list:'',
	category_id: "",	//分类
	pid: "",
	pinyin: "",
	brand: "",	//商品品牌
	pics: [],
	pic: "",
	describe: "",
	txa_code:"",
	standard_code: "",	//标准码
	is_standard: 1,		//是否标品
	detail: "",
	transfer: 1,	//互调比
  },
};

export default {
  components: {
	Editor,
  },
  filters: {
	unit(id, arry) {
	  if (arry.unit) {
		let name = "";
		arry.unit.forEach((e) => {
		  if (e.id == id) {
			name = e.title;
		  }
		});
		return name;
	  }
	},
  },
  data() {
	// var checkName = (rule, value, callback) => {
	// 	console.log('???',rule,value)
	// 	if (value == this.monitorForm.form.name) {
	// 		callback(new Error('其它名称不能与商品名称相同'));
	// 	} else {
	// 		callback();
	// 	}
	// };
	return {
		id:null,
		nostand:null,	//是否是非标准库
		imgBase:'',		//图片baseurl
		title:'新增商品',	//头部标题
		saveText:'保存并返回列表',	//保存按钮的文本
		//验证别名是否跟主名称一样
		nameSame:false,
		//其它名称列表
		nameList:[{name:''}],
		// SKU列表数据
		monitorForm: {
			list: [
				{
					base: 1,
					spu_name: "",
					unit: "",
					package_num: 1,
					transfer_unit: "",
					transfer_num: 0,
					is_float: true,
					standard_spu_code: "",
					down: false,
					status: true,
				},
			],
			form: {
				name: "",
				name_list:[''],
				category_id: "",
				pid: "",
				pinyin: "",
				standard_code: "",
				brand: "",
				pics: [],
				pic: "",
				describe: "",
				txa_code:"",
				is_standard: 1,
				detail: "",
				transfer: 1,
			},
		},
		// 初始列表数据
		inlist: [
			{
			base: 1,
			spu_name: "",
			unit: "",
			package_num: 1,
			transfer_unit: "",
			transfer_num: 0,
			is_float: true,
			standard_spu_code: "",
			down: false,
			},
		],
		// 富文本
		init,
		// 验证
		rules,
		// 上传图片
		filelist: [],
		uploadPath,
		dialogImageUrl: "",
		dialogVisible: false,
		// 分类
		options: [],
		defaultProps: {
			children: "selfNext",
			label: "title",
			value: "id",
		},
		// 信息
		info: {},
		// 监听滚动
		searchBarFixed: false,
		// 商品名称标准库
		show: false,

		tally:null,		//导入次数
	};
  },
  mounted() {
	this.imgBase = this.$store.state.user.avatar.img_prefix;

	this.id = this.$route.query.id;
	this.nostand = this.$route.query.nostand || null;
	this.goods_base();
	if(this.id){
		if(this.nostand){
			// 非标准库(导入标准库)
			this.getDetail(this.id,false);
			this.title = '导入标准库'
			this.saveText = '导入标准库'
			this.$route.meta.bread = "非标准库,导入标准库"
		}else{
			//标准库(修改)
			this.getDetail(this.id,true)
			this.title = '编辑商品'
			this.$route.meta.bread = "标准库,编辑商品"
		}
	}else{
		//新增
		this.title = '新增商品'
		this.$route.meta.bread = "标准库,新增商品"
	}

	window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
	window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
	checkName(val){
		var nameArr = [];
		//判断别名是否跟名称相同
		for(let i in this.nameList){
			if( this.nameList[i].name == this.monitorForm.form.name){
				this.nameSame = true;
				return;
			}
			nameArr.push(this.nameList[i].name)
		}
		//判断是否有别名重复
		for(let i=0;i <= this.nameList.length-2;i++){
			for(let y = i+1;y <=this.nameList.length-1;y++){
				if(this.nameList[i].name == this.nameList[y].name) {
					this.nameSame = true;
					return;
				}
			}
		}
		this.nameSame = false;
	},
	getDetail(id,isstand){
		let data = Object.assign(
			{
				sign: Md5(
				objKeySort(
					Object.assign(apiUrl().name, { id })
				) + apiUrl().appsecret
				),
			},
			apiUrl().name,
			{id}
		);
		if(isstand){
			//标准库
			standardDetail(data).then((res) => {
				if (res.errcode == 0) {
					var data = res.data.info;
					this.monitorForm.form = {
						name : data.name,
						category_id: [data.pid,data.category_id],
						pid: data.pid,
						pinyin: data.pinyin,
						standard_code: data.standard_code,
						brand: data.brand ? Number(data.brand) : '',
						// pics: data.pics ? data.pics.split(',') : [],
						pic: data.pic ? data.pic :'',
						describe: data.describe ? data.describe : '',
						txa_code: data.txa_code ? data.txa_code : '',
						is_standard: data.is_standard,
						detail: data.detail ? data.detail : '',
						transfer: data.transfer,
					}
					//导入次数。 大于0不能修改商品分类
					this.tally = data.tally;
					if (data.pics) {
						var arr = []
						data.pics.split(",").map((e) => {
							arr.push({ name: "", path: e, url: `${this.imgBase}${e}` });
						});
						this.monitorForm.form.pics = arr;
						this.filelist = arr;
					}else{
						this.monitorForm.form.pics = []
					}
					//其它名称
					var arr = data.standard_name;
					// for(let i in data.standard_name){
					// 	arr.push(data.standard_name[i].name);
					// }
					this.nameList = arr.length > 0 ? arr : [{name:''}];
					this.monitorForm.list = data.spu.map(item=>{
						return {
							id:item.id,
							base: item.base,
							spu_name: item.spu_name.id,
							unit: item.unit,
							package_num: item.package_num,
							transfer_unit: item.transfer_unit,
							transfer_num: item.transfer_num,
							is_float: item.is_float,
							standard_spu_code: item.standard_spu_code,
							down: false,
							status: item.status,
							candel:false,
						}
					})
					// console.log(this.monitorForm.list)
				}
			})
		}else{
			nonStandardDetail(data).then((res) => {
				if (res.errcode == 0) {
					var data = res.data.info;
					this.monitorForm.form = {
						name : data.name,
						category_id: [data.pid,data.category_id],
						pid: data.pid,
						pinyin: data.pinyin,
						goods_code: data.goods_code,
						brand: data.brand ? Number(data.brand) : '',
						// pics: data.pics ? data.pics : [],
						pic: data.pic ? data.pic :'',
						describe: data.describe ? data.describe : '',
						txa_code: data.txa_code ? data.txa_code : '',
						is_standard: data.is_standard,
						detail: data.detail ? data.detail : '',
						transfer: data.transfer,
					}
					if (data.pics) {
						var arr = []
						data.pics.split(",").map((e) => {
							arr.push({ name: "", path: e, url: '' });
						});
						this.monitorForm.form.pics = arr;
					}else{
						this.monitorForm.form.pics = []
					}
					this.nameList = [{name:''}];
					this.monitorForm.list = data.spu.map(item=>{
						return {
							id:item.id,
							base: item.base,
							spu_name: item.spu_name,
							unit: item.unit,
							package_num: item.package_num,
							transfer_unit: item.transfer_unit,
							transfer_num: item.transfer_num,
							is_float: item.is_float,
							goods_spu_code: item.goods_spu_code,
							down: false,
							status: item.status,
						}
					})
				}
			})
		}
	},
	//添加或删除其它名称列表
	AddOrSubNameList(index){
		if(index){
			//删除一行
			this.nameList.splice(index,1)
		}else{
			this.nameList.push({name:''})
		}
	},
	// 监听滚动
	handleScroll() {
	  let scrollTop =
		window.pageYOffset ||
		document.documentElement.scrollTop ||
		document.body.scrollTop;
	  scrollTop + document.documentElement.clientHeight >
	  document.getElementById("newConBox").offsetTop + 300
		? (this.searchBarFixed = false)
		: (this.searchBarFixed = true);
	},
	// // 标准编码
	// checkboxGroup(val) {
	//   this.monitorForm.form.use_code = [];
	//   this.monitorForm.form.use_code.push(val);
	// },
	// 焦点离开商品
	inputBlur(val,type,index) {
	  if (val.target.value != "") {
		  var params = {name:val.target.value};
		  if(type == 'main'){
			  Object.assign(params,{'standard_id':this.id})
		  }else if(type == 'other'){
			  Object.assign(params,{'name_id':this.nameList[index].id})
		  }
		let data = Object.assign(
		  {
			sign: Md5(
			  objKeySort(
				Object.assign(apiUrl().name, params)
			  ) + apiUrl().appsecret
			),
		  },
		  apiUrl().name,
		  params
		);
		goods_check(data).then((res) => {
		  if (res.errcode == 0) {
		  } else if (res.errcode == 40017) {
			this.$alert("导入标准库商品", "提示", {
			  showClose: false,
			  confirmButtonText: "确定",
			  callback: (action) => {
				this.show = true;
				this.standard(res.data, 1);
			  },
			}).catch(() => {});
		  } else if (res.errcode == 40018) {
			this.$confirm("导入非标准库商品", "提示", {
			  confirmButtonText: "确定",
			  cancelButtonText: "取消",
			  type: "warning",
			})
			  .then(() => {
				this.standard(res.data, 2);
			  })
			  .catch(() => {});
		  } else {
			this.$message.error(res.errmsg);
		  }
		});
	  } else {
		// this.$nextTick(() => {
		//   if (val.target.offsetParent) {
		// 	val.target.offsetParent.querySelector("input").focus();
		//   }
		// });
	  }
	},
	// 获取首字母
	inputName(val) {
	  let gs = vPinyin.chineseToPinYin(val);
	  let SX = "";
	  let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
	  if (!/^(\d+\.?)?\d{0,2}$/.test(gs)) {
		if (reg.test(val)) {
		  for (var i = 0; i < gs.length; i++) {
			var c = gs.charAt(i);
			if (/^[A-Z]+$/.test(c)) {
			  SX += c;
			}
		  }
		} else {
		  for (var i = 0; i < gs.length; i++) {
			var c = gs.charAt(i);
			SX += c.toUpperCase();
		  }
		}
	  }
	  this.monitorForm.form.pinyin = SX;
	},
	// 取消展开行
	toogleExpand_qx(row, index) {
	  let $table = this.$refs.multipleTable;
	  this.monitorForm.list[index].down = false;
	  $table.toggleRowExpansion(row, false);
	},
	// 展开行
	toogleExpand(row, index) {
	  let $table = this.$refs.multipleTable;
	  this.monitorForm.list[index].down = !this.monitorForm.list[index].down;
	  this.monitorForm.list.map((item) => {
		if (item.down) {
		  $table.toggleRowExpansion(item, true);
		} else {
		  $table.toggleRowExpansion(item, false);
		}
	  });
	},
	// SKU设置
	radioGrou(val) {
	  this.monitorForm.list = JSON.parse(JSON.stringify(this.inlist));
	},
	// SKU设置-加
	plus() {
	  let $table = this.$refs.multipleTable;
	  shuju.list.base = 0;
	  shuju.list.candel = true;
	  this.monitorForm.list.push(JSON.parse(JSON.stringify(shuju.list)));
	  this.inlist.push(JSON.parse(JSON.stringify(shuju.list)));
	  this.$nextTick(() => {
		this.monitorForm.list.map((item) => {
		  if (item.down) {
			$table.toggleRowExpansion(item, true);
		  } else {
			$table.toggleRowExpansion(item, false);
		  }
		});
	  });
	},
	// SKU设置-减
	reduce(index) {
	  let $table = this.$refs.multipleTable;
	  this.monitorForm.list.splice(index, 1);
	  this.inlist.splice(index, 1);
	  this.$nextTick(() => {
		this.monitorForm.list.map((item) => {
		  if (item.down) {
			$table.toggleRowExpansion(item, true);
		  } else {
			$table.toggleRowExpansion(item, false);
		  }
		});
	  });
	},
	// 上传图片-删除
	handleRemove(file) {
	  const i = this.monitorForm.form.pics.findIndex(
		(item) => item.path == file.path
	  );
	  this.monitorForm.form.pics.splice(i, 1);
	},
	// 上传图片-查看
	handlePictureCardPreview(file) {
	  this.dialogImageUrl = file.url;
	  this.dialogVisible = true;
	},
	// 上传图片-成功
	handSuccess(res, file, fileList) {
		this.monitorForm.form.pics.push({
		  name: "",
		  path: res.data.path,
		  url: res.data.src,
		});
		//文件过大上传失败是接口拒绝，但是组件还是默认上传成功了，所以要先加上数据上传失败再删掉
		if(res.errcode != 0){
			this.$message.error(res.errmsg);
			this.handleRemove(file)
		}
	},
	// 保存
	preservation(type) {
	  this.$refs["userListFrom"].validate((valid) => {
		if (valid && !this.nameSame) {
			let arry = JSON.parse(JSON.stringify(this.monitorForm.form));
			let sz = JSON.parse(JSON.stringify(this.monitorForm.list));
			let list = sz.map((a) => {
				a.transfer_unit = a.transfer_unit ? a.transfer_unit : 0;
				let { down,candel,  ...sz } = a;
				return { ...sz };
			});
			arry.is_standard = arry.is_standard ;
			arry.pid = arry.category_id[0];
			arry.category_id = category(arry.category_id);
			arry.txa_code = arry.txa_code ? arry.txa_code : '';
			arry.pic = arry.pics.length > 0 ? arry.pics[0].path : '';
			arry.pics = arry.pics.length > 0 ? arry.pics.map((p) => p.path).join(",") : '';
			arry.name_list = this.nameList.map(item=>{
				return item.name
			}).toString()
			arry.id = this.id;
			let data = Object.assign(
				{
				sign: Md5(
					objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
				),
				},
				apiUrl().name,
				arry,
				{ list: JSON.stringify(list) }
			);
			if(this.id){
				if(this.nostand){
					//非标准库导入

					importStandard(data).then(res=>{
						if (res.errcode == 0) {
							this.$message({
								message: "导入成功",
								type: "success",
							});
							this.$router.go(-1);
						} else {
							this.$message.error(res.errmsg);
						}
					})
				}else{
					//标准库编辑
					goods_edit(data).then((res) => {
						if (res.errcode == 0) {
							this.$message({
								message: "编辑成功",
								type: "success",
							});
							this.$router.go(-1);
						} else {
							this.$message.error(res.errmsg);
						}
					});

				}
			}else{
				//新增
				goods_add(data).then((res) => {
					if (res.errcode == 0) {
					this.$message({
						message: "新增成功",
						type: "success",
					});
					if (type == 1) {
						this.nameList = [{name:''}];
						this.monitorForm.list = [];
						this.monitorForm.list.push(
						JSON.parse(JSON.stringify(shuju.list))
						);
						this.monitorForm.form = JSON.parse(JSON.stringify(shuju.form));
					} else {
						this.$router.go(-1);
					}
					} else {
						this.$message.error(res.errmsg);
					}
				});
			}
		}
	  });
	},
	// 下拉信息
	goods_base() {
	  let data = Object.assign(
		{
		  sign: Md5(
			objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
		  ),
		},
		apiUrl().name
	  );
	  goods_base(data).then((res) => {
		if (res.errcode == 0) {
		  this.options = recursion(res.data.category);
		  this.info = res.data;
		} else {
		  this.$message.error(res.errmsg);
		}
	  });
	},
	standard(data, type) {
	  let arry = [];
	//   let use_code = [];
	//   use_code.push(parseInt(data.info.use_code));
	  data.info.spu.map((e) => {
		e.spu_name = parseInt(e.spu_name);
		e.transfer_unit = e.transfer_unit ? parseInt(e.transfer_unit) : "";
		e.down = false;
	  });
	  if (data.info.pics) {
		data.info.pics.split(",").map((e) => {
		  arry.push({ name: "", path: e, url: data.domain + e });
		});
	  }
	//   this.monitorForm.form.use_code = use_code;
	  this.monitorForm.form.name = data.info.name;
	  this.monitorForm.form.category_id = [
		data.info.pid,
		data.info.category_id,
	  ];
	  this.monitorForm.form.pid = data.info.pid;
	  this.monitorForm.form.pinyin = data.info.pinyin;
	  this.monitorForm.form.standard_code = data.info.standard_code;
	  this.monitorForm.form.brand = parseInt(data.info.brand);
	  this.monitorForm.form.pics = JSON.parse(JSON.stringify(arry));
	  this.filelist = JSON.parse(JSON.stringify(arry));
	  this.monitorForm.form.pic = data.info.pic;
	  this.monitorForm.form.describe = data.info.describe;
	//   this.monitorForm.form.txa_code = data.info.txa_code;
	  this.monitorForm.form.is_standard = data.info.is_standard;
	  this.monitorForm.form.transfer = data.info.transfer;
	  this.monitorForm.list = JSON.parse(JSON.stringify(data.info.spu));
	  this.inlist = JSON.parse(JSON.stringify(data.info.spu));
	  this.monitorForm.form.detail = data.info.detail;
	  this.monitorForm.form.standard_code = data.info.standard_code;
	  if (type == 1) {
		this.monitorForm.form.standard_code = data.info.standard_code;
	  }
	//   if (type == 2) {
	// 	// this.monitorForm.form.nonstandard_goods_id = data.info.id;
	//   }
	},
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.wid120 /deep/.el-input__inner{
	width:120rpx!important;
}
.name_list{
	.maincla_sxf{
		svg{
			margin-left:20px;
			fill:#c1c1c1ff;
		}
	}

}
.maincla_form {
  /deep/.el-table .cell .el-table__expand-icon {
	display: none;
  }
  margin-left: 10px;
  margin-top: 10px;
  /deep/.el-form-item--mini.el-form-item,
  /deep/.el-form-item--small.el-form-item {
	margin-bottom: 22px;
  }
  .label_ids {
	display: block;
	/deep/.el-form-item__content {
	  width: 85%;
	}
  }
  .maincla_lea {
	font-size: 0;
	span {
	  min-width: 70px;
	  padding-left: 10px;
	  padding-right: 10px;
	  height: 32px;
	  line-height: 30px;
	  display: inline-block;
	  vertical-align: middle;
	  align-items: center;
	  text-align: center;
	  font-size: $font_14;
	  color: $colour_3;
	  border: 1px solid $subActive;
	  &:hover {
		cursor: pointer;
	  }
	  &.on {
		border-color: $menuActiveText;
		color: $menuActiveText;
	  }
	}
  }
  .main_tile {
	margin-left: -10px;
  }
  .maincla_te {
	width: 59.7%;
	/deep/.el-form-item__content {
	  width: calc(100% - 110px);
	}
  }
  .maincla_ed {
	margin-top: 20px;
	margin-left: 10px;
	width: 66%;
  }
  .maincla_ra {
	margin-top: 4px;
	margin-left: 30px;
	/deep/.el-radio {
	  margin-right: 15px;
	}
  }
  .main_table {
	/deep/.el-table--small td {
	  padding-top: 0;
	}
	/deep/.el-form-item--small.el-form-item {
	  margin-bottom: 16px;
	  margin-top: 16px;
	  margin-right: 0;
	}
	.maincla_sx {
	  .maincla_sxf {
		margin-right: 15px;
		margin-left: 10px;
		margin-top: 2px;
		svg {
		  display: block;
		  &:nth-child(2) {
			margin-left: -4px;
			margin-top: 5px;
		  }
		  &:hover {
			cursor: pointer;
		  }
		}
		use {
		  fill: #c1c1c1ff;
		}
	  }
	  .maincla_sxs {
		width: 80px;
		margin-right: 20px;
		color: $colour_3;
	  }
	}
	.maincla_dw {
	  .maincla_dwp {
		.maincla_dwp_un {
		  min-width: 90px;
		  text-align: left;
		  margin-right: 10px;
		}
		.wid110 /deep/.el-input__inner {
		  width: 90px;
		  padding-left: 0px;
		}
		/deep/.el-select .el-input .el-select__caret {
		  font-weight: bold;
		  color: $colour_3;
		}
		/deep/.el-input__inner {
		  border: 0;
		  background: none;
		}
		strong {
		  width: 40px;
		  line-height: 16px;
		  display: inline-block;
		  vertical-align: middle;
		  color: $menuActiveText;
		  margin-left: -7px;
		}
		&:hover {
		  cursor: pointer;
		  & strong {
			text-decoration: underline;
		  }
		}
	  }
	  p {
		i {
		  font-size: 18px;
		  color: $menuText;
		}
	  }
	}
	.maincla_bz {
	  input {
		margin-right: 5px;
	  }
	}
	/deep/.el-table tr.down td {
	  border-bottom: 0;
	}
	.maincla_zh {
	  margin-left: 517px;
	  margin-bottom: 5px;
	  margin-top: 10px;
	  align-items: center;
	  & > span {
		margin-right: 5px;
	  }
	  /deep/.el-select--small {
		margin-left: 5px;
	  }
	  p {
		color: $colour_red;
		margin-left: 10px;
		&:hover {
		  cursor: pointer;
		}
	  }
	}
  }
}
</style>
